class_name PlayerStateMachine
extends GameplayStateMachine

@onready var player: Player = $"../"


func _on_setup() -> void:
	var ground_transition := ConditionStateTransition.new(
		"GroundState",
		func(): return player.state_checker.is_on_ground,
		func(): pass
	)
	add_transition("GhostState", ground_transition)
	add_transition("AirState", ground_transition)
	
	var air_transition := ConditionStateTransition.new(
		"AirState",
		func(): return not player.state_checker.is_on_ground,
		func(): pass
	)
	add_transition("GhostState", air_transition)
	add_transition("GroundState", air_transition)

	var climb_transition := ConditionStateTransition.new(
		"ClimbState",
		func():
			return\
				(player.state_checker.is_up_ladder_detected and player.input_handler.is_action_pressed("climb")) or\
				(player.state_checker.is_down_ladder_detected and player.input_handler.is_action_pressed("crouch")),
		func(): pass
	)
	add_transition("AnyState", climb_transition)

	var end_climb_transition := ConditionStateTransition.new(
		"GhostState",
		func(): 
			return\
				(player.input_handler.is_action_pressed("cancel_climb")) or\
				(not player.state_checker.is_up_ladder_detected and not player.state_checker.is_down_ladder_detected) or\
				(not player.state_checker.is_up_ladder_detected and player.input_handler.is_action_pressed("climb")) or\
				(not player.state_checker.is_down_ladder_detected and player.input_handler.is_action_pressed("crouch")),
		func(): pass
	)
	add_transition("ClimbState", end_climb_transition)

	var hurt_transition := EventStateTransition.new("HurtState", "Hurt", func(): pass)
	add_transition("AnyState", hurt_transition)


func _ready() -> void:
	on_setup()


func _process(delta: float) -> void:
	on_update(delta)
